package com.commerceE.managedBean;

import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.RowEditEvent;

import com.commerceE.common.exception.ServiceException;
import com.commerceE.domain.Category;
import com.commerceE.domain.ProductTag;
import com.commerceE.domain.Products;
import com.commerceE.service.ProductService;
import com.commerceE.service.ProductTagService;
import com.commerceE.util.SpringUtil;

@ManagedBean(name = "productTagBean")
@RequestScoped
public class ProductTagBean {
	
	@ManagedProperty(value = "#{adminHomeBean}")
	private AdminHomeBean adminHomeBean;
	
	private ProductTagService productTagService;
	
	private ProductService productService;
	
	private List<ProductTag> productTagList;
	
	private ProductTag selectedProductTag;
	
	private ProductTag newProductTag;
	
	private Products selectedProduct;
	
	private String search_tagType;
	
	private List<Products> productList;
	
	public ProductTagBean(){
		productTagService = (ProductTagService) SpringUtil
				.getBeanById("productTagService");
		productService = (ProductService) SpringUtil
				.getBeanById("productService");
		try {
			productList = productService.getAllListOfProduct();
			productTagList = productTagService.getProductTagListByTagType(search_tagType);
			System.out.println(productTagList.size());
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		newProductTag =new ProductTag();
	}
	
	public void onAddInput(){
		adminHomeBean.setCenterPage("/xhtml/productTagInput.xhtml");
	}
	
	
	public void onAddSubmit(){
		if(newProductTag!=null){
			try {
				productTagService.insertProductTag(newProductTag);
			} catch (ServiceException e) {
				FacesMessage msg = new FacesMessage(e.getMessage(),
						"");
				FacesContext.getCurrentInstance().addMessage(null, msg);
				e.printStackTrace();
				return;
			}
			try {
				productTagList = productTagService.getProductTagListByTagType(search_tagType);
			} catch (ServiceException e) {
				e.printStackTrace();
			}
			adminHomeBean.setCenterPage("/xhtml/productTagList.xhtml");
		}
	}
	
    public void onEditInput(){
    	if (selectedProductTag == null) {
			FacesMessage msg = new FacesMessage("Please select 1 row first!",
					"");
			FacesContext.getCurrentInstance().addMessage(null, msg);
		} else {
			adminHomeBean.setCenterPage("/xhtml/productTagInput.xhtml");
		}
	}
	
	
	public void onEditSubmit(RowEditEvent event) { 
		ProductTag pt = (ProductTag) event.getObject();
		FacesMessage msg = null;
		try {
			productTagService.updateProductTag(pt);
		} catch (ServiceException e) {
			e.printStackTrace();
			 msg = new FacesMessage(e.getMessage(), ""+pt.getProductTagId());
		}
		if(msg==null){
			msg = new FacesMessage("Product Tag Edited. ", ""+pt.getProductTagId());
		}
        FacesContext.getCurrentInstance().addMessage(null, msg);  
	}
	
	public void onDelete(){
		if (selectedProductTag == null) {
			FacesMessage msg = new FacesMessage("Please select 1 row first!",
					"");
			FacesContext.getCurrentInstance().addMessage(null, msg);
		} else {
			try {
				productTagService.deleteProductTag(selectedProductTag);
			} catch (ServiceException e) {
				e.printStackTrace();
			}
			try {
				productTagList = productTagService.getProductTagListByTagType(search_tagType);
			} catch (ServiceException e) {
				e.printStackTrace();
			}
			adminHomeBean.setCenterPage("/xhtml/productTagList.xhtml");
		}
	}
	
	public void chooseProduct(){
		if(selectedProduct!=null){
			newProductTag.setProductId(selectedProduct.getProductId());
		}
	}

	
	
	
	//setters and getters
	public ProductTagService getProductTagService() {
		return productTagService;
	}

	public void setProductTagService(ProductTagService productTagService) {
		this.productTagService = productTagService;
	}

	public List<ProductTag> getProductTagList() {
		return productTagList;
	}

	public void setProductTagList(List<ProductTag> productTagList) {
		this.productTagList = productTagList;
	}

	public ProductTag getSelectedProductTag() {
		return selectedProductTag;
	}

	public void setSelectedProductTag(ProductTag selectedProductTag) {
		this.selectedProductTag = selectedProductTag;
	}

	public ProductTag getNewProductTag() {
		return newProductTag;
	}

	public void setNewProductTag(ProductTag newProductTag) {
		this.newProductTag = newProductTag;
	}

	public ProductService getProductService() {
		return productService;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public Products getSelectedProduct() {
		return selectedProduct;
	}

	public void setSelectedProduct(Products selectedProduct) {
		this.selectedProduct = selectedProduct;
	}

	public List<Products> getProductList() {
		return productList;
	}

	public void setProductList(List<Products> productList) {
		this.productList = productList;
	}

	public String getSearch_tagType() {
		return search_tagType;
	}

	public void setSearch_tagType(String search_tagType) {
		this.search_tagType = search_tagType;
	}

	public AdminHomeBean getAdminHomeBean() {
		return adminHomeBean;
	}

	public void setAdminHomeBean(AdminHomeBean adminHomeBean) {
		this.adminHomeBean = adminHomeBean;
	}
	
}
